#include "form_inquiry_dlg.h"
#include "ui_form_inquiry_dlg.h"

static int s_nReplyType = 0;

/*
 * Function:    FormInquiryWidget
 * Description: The default constructor
 * Paramer:     void
 * Return:
 */
FormInquiryWidget::FormInquiryWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormInquiryWidget)
{
    qDebug() << Q_FUNC_INFO;

    ui->setupUi(this);
}

/*
 * FormInquiryWidget:    ~FormInquiryWidget
 * Description: The default destructor
 * Paramer:     void
 * Return:
 */
FormInquiryWidget::~FormInquiryWidget()
{
    qDebug() << Q_FUNC_INFO;

    delete ui;
}

/*
 * Function:    getInstance
 * Description: Get an instance of "FormInquiryWidget" class, if exist, return it
 * Paramer:     QWidget *pParent - the pointer of QWidget class
 * Return:      the pointer of "FormStartupTest" class
 */
FormInquiryWidget* FormInquiryWidget::getInstance(QWidget *pParent)
{
    static FormInquiryWidget *pNew = nullptr;
    if(!pNew) {
        qDebug() << Q_FUNC_INFO;

        // Create a new object if it doesn't exist
        pNew = new FormInquiryWidget(pParent);
        if(NULL == pNew) return NULL;

        // After constructing object, initialize the object
        pNew->_Initialze();

        // Initialize control's UI
        pNew->_InitUi();

        // Hide widget firstly
        pNew->hide();
    }
    else {
        // Set the parent object
        pNew->setParent(pParent);
    }

    return pNew;
}

/*
 * Function:    _Initialze
 * Description: After constructing object, initialize the object
 * Paramer:     void
 * Return:      void
 */
void FormInquiryWidget::_Initialze()
{
    qDebug() << Q_FUNC_INFO;
}

/*
* Function:		_InitUi
* Description:	Init the window style
* Paramer:		void
* Return:		void
*/
void FormInquiryWidget::_InitUi()
{
    qDebug() << Q_FUNC_INFO;

    // Displays the background image of the window
    QWidget::setAttribute(Qt::WA_StyledBackground);

    ui->label_Prompt_Info->setWordWrap(true);

    SET_STYLE_SHEET(QString(":/%1/share/%2.qss").arg("skin_1").arg(this->objectName()));

    // Adjust the position
    this->move(0, 0);
    this->hide();
}

/*
* Function:		changeEvent
* Description:	Triggers an event before it is switch language
* Paramer:		QShowEvent *event - the pointer of "QEvent"
* Return:		void
*/
void FormInquiryWidget::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);

    switch (e->type())
    {
    case QEvent::LanguageChange:
        qDebug() << Q_FUNC_INFO;

        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

/*
* Function:		on_pushButton_Save_clicked
* Description:	Save the basic info after click the button
* Paramer:		void
* Return:		void
*/
void FormInquiryWidget::on_pushButton_Confirm_clicked()
{
    qDebug() << Q_FUNC_INFO << s_nReplyType;
    qDebug() << Q_FUNC_INFO << this << this->parent();

    ui->pushButton_Confirm->setEnabled(false);
    ui->pushButton_Cancel->setEnabled(false);

    QTimer::singleShot(100, this, [&]() {
        // Signal-slot initialization
        bool bCon = QObject::connect(this, SIGNAL(signalReplayInfo(int, bool)), this->parent(), SLOT(slotRespondInfoFromDlg(int, bool)), Qt::AutoConnection);
        Q_ASSERT(bCon);

        this->hide();

        emit signalReplayInfo(s_nReplyType, true);

        // Signal-slot disconnect
        QObject::disconnect(this, SIGNAL(signalReplayInfo(int, bool)), this->parent(), SLOT(slotRespondInfoFromDlg(int, bool)));
    });
}

/*
* Function:		on_pushButton_Cancel_clicked
* Description:	Close the window after click the button
* Paramer:		void
* Return:		void
*/
void FormInquiryWidget::on_pushButton_Cancel_clicked()
{
    qDebug() << Q_FUNC_INFO;

    ui->pushButton_Confirm->setEnabled(false);
    ui->pushButton_Cancel->setEnabled(false);

    QTimer::singleShot(100, this, [&]() {
        this->hide();
    });
}

/*
 * Function:    SetInquiryInfo
 * Description: Set the inquiry info on window
 * Paramer:     QString sInquiryInfo - the inquiry info
 *              int nReplyType - the inquiry type
 * Return:      void
 */
void FormInquiryWidget::setInquiryInfo(QString sInquiryInfo, int nInquiryType)
{
    qDebug() << Q_FUNC_INFO << sInquiryInfo << nInquiryType;

    s_nReplyType = nInquiryType;

    // Update the prompt info
    ui->label_Prompt_Info->setText(sInquiryInfo);
    ui->pushButton_Confirm->setEnabled(true);
    ui->pushButton_Cancel->setEnabled(true);

    this->show();
}
